<?php
namespace app\index\model;
use think\Db;
use think\Model;

class Collect_model {
	// 我的收藏
	public function getindex() {
		// $uid = (session('username')[0]['user_id']);
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		//记录用户操作
		$data = [
			'user_id' => $uid,
			'log_action' => 'Collect/index',
			'log_desc' => '我的收藏',
			'create_time' => $time,
			'is_delete' => 0, //默认0
		];
		$res = db('_user_log')
			->insert($data);

		$list = Db::table('cn_archives')
			->alias('ca')
			->join('cn_collect_archives_relation ccar', 'ccar.arc_id = ca.arc_id')
			->join('cn_collect cc', 'ccar.col_id = cc.col_id')
			->field('ca.arc_name,ca.arc_file_size,ccar.update_time,ca.arc_id,ca.arc_type,ca.arc_desc,ca.audit_status')
			->where('cc.create_user_id', $uid)
			->where('ca.is_delete', 'eq', '0')
			->where('cc.is_delete', 'eq', '0')
			->order('ccar.update_time desc')
			->paginate(10);

		$page = $list->render();

		$info = [
			'list' => $list,
			'page' => $page,
		];

		$ppp = $list->toArray();
		if (count($ppp['data']) == 0) {
			return false;
		} else {
			return $info;
		}

	}

	// 取消收藏
	public function cancelcol() {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$id = $_POST['id'];
		// 取消收藏
		$cancel = Db::table('cn_archives')
			->alias('ca')
			->join('cn_collect_archives_relation ccar', 'ccar.arc_id = ca.arc_id')
			->join('cn_collect cc', 'cc.col_id = ccar.col_id')
			->where('ca.arc_id', $id)
			->update(['cc.is_delete' => '1']);
		$ccar_cancel = Db::table('cn_collect_archives_relation')
			->alias('ca')
			->join('cn_collect_archives_relation ccar', 'ccar.arc_id = ca.arc_id')
			->where('ca.arc_id', $id)
			->update(['ccar.is_delete' => '1']);
		if ($cancel && $ccar_cancel) {
			return $cancel;
		} else {
			return false;
		}
	}

}

?>